package com.augurit.gzcgwsp.webservice.service.impl;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.springside.modules.orm.Page;

import com.augurit.gzcgwsp.webservice.dao.ExChangeDataDao;
import com.augurit.gzcgwsp.webservice.service.IKingdomDataService;


public class KingdomDataServiceImpl implements IKingdomDataService{
	
	private static final Logger log=Logger.getLogger(KingdomDataServiceImpl.class);
	
	private static final  ExChangeDataDao ExChangeDataDao=new ExChangeDataDao();

	/*
	 * 生成户外广告的数据(non-Javadoc)
	 * @see com.augurit.gzcgwsp.webservice.service.IDataService#getDatas(java.lang.String, java.lang.String)
	 */
	@Override
	public String  getHwggDatas(String pageNo, String pageSize) {
		if(StringUtils.isBlank(pageNo)){
			pageNo="1";
		}
		if(StringUtils.isBlank(pageSize)){
			pageSize="10";
		}
		List list=this.getHwgg(pageNo, pageSize);
		String sql=this.getHwggSql();
		Long totalcount=ExChangeDataDao.getTotalCount(sql);
		Document doc = DocumentHelper.createDocument();  
		Element root = doc.addElement("hwggs");   
	    root.addElement("totalcount").addText(String.valueOf(totalcount));
		if(list.size()>0){
			for(int i=0 ;i<list.size();i++){
				Map map=(Map) list.get(i);
				Element hwgg=root.addElement("hwgg");
				this.creatHwggXml(map, hwgg);
			}
		}
		return this.formatXML(doc, "UTF-8");
	}
	
	/*
	 * 生成经营许可的内容(non-Javadoc)
	 * @see com.augurit.gzcgwsp.webservice.service.IDataService#getDatas(java.lang.String, java.lang.String)
	 */
	
	public String  getRqjyxkDatas(String pageNo, String pageSize) {
			if(StringUtils.isBlank(pageNo)){
				pageNo="1";
			}
			if(StringUtils.isBlank(pageSize)){
				pageSize="10";
			}
		  List list=this.getJyxk(pageNo, pageSize);
		  String sql=this.getJyxkSql();
		  Long totalcount=ExChangeDataDao.getTotalCount(sql);
		 Document doc = DocumentHelper.createDocument();  
		 Element root = doc.addElement("jyxks");   
		 root.addElement("totalcount").addText(String.valueOf(totalcount));
		if(list.size()>0){
			for(int i=0 ;i<list.size();i++){
				Map map=(Map) list.get(i);
				Element jyxk=root.addElement("jyxk");
			 this.creatJyxkXml(map, jyxk);
			}
		}
		System.out.print(this.formatXML(doc,"UTF-8"));
		return this.formatXML(doc,"UTF-8");
		//return doc.asXML();
	}
	
	public String  getYnYsqyDatas(String pageNo, String pageSize) {
		if(StringUtils.isBlank(pageNo)){
			pageNo="1";
		}
		if(StringUtils.isBlank(pageSize)){
			pageSize="10";
		}
		List list=this.getYnYsqy(pageNo, pageSize);
		String sql=this.getYnYsqySql();
		Long totalcount=ExChangeDataDao.getTotalCount(sql);
		Document doc = DocumentHelper.createDocument();  
		Element root = doc.addElement("ynysqys");   
		root.addElement("totalcount").addText(String.valueOf(totalcount));
		if(list.size()>0){
			for(int i=0 ;i<list.size();i++){
				Map map=(Map) list.get(i);
				Element ynysqy=root.addElement("ynysqy");
				this.creatYnYsqyXml(map, ynysqy);
			}
		}
		System.out.print(this.formatXML(doc,"UTF-8"));
		return this.formatXML(doc,"UTF-8");
		//return doc.asXML();
	}
	
	public String  getYnYsclDatas(String pageNo, String pageSize) {
		if(StringUtils.isBlank(pageNo)){
			pageNo="1";
		}
		if(StringUtils.isBlank(pageSize)){
			pageSize="10";
		}
		List list=this.getYnYscl(pageNo, pageSize);
		String sql=this.getYnYsclSql();
		Long totalcount=ExChangeDataDao.getTotalCount(sql);
		Document doc = DocumentHelper.createDocument();  
		Element root = doc.addElement("ynyscls");   
		root.addElement("totalcount").addText(String.valueOf(totalcount));
		if(list.size()>0){
			for(int i=0 ;i<list.size();i++){
				Map map=(Map) list.get(i);
				Element ynyscl=root.addElement("ynyscl");
				this.creatYnYsclXml(map, ynyscl);
			}
		}
		System.out.print(this.formatXML(doc,"UTF-8"));
		return this.formatXML(doc,"UTF-8");
		//return doc.asXML();
	}
	
	/*
	 * 企业资质
	 * @see com.augurit.gzcgwsp.webservice.service.IKingdomDataService#getRqQyzzDatas(java.lang.String, java.lang.String)
	 */
	@Override
	public String getRqQyzzDatas(String pageNo, String pageSize) {
		
		return null;
	}
	
	
	
	
	/**
	 * 格式化xml
	 * @param document
	 * @param charset
	 * @return
	 */
	public  String formatXML(Document document, String charset) { 
        OutputFormat format = OutputFormat.createPrettyPrint(); 
        format.setEncoding(charset); 
        //format= OutputFormat.createCompactFormat();
        StringWriter sw = new StringWriter(); 
        XMLWriter xw = new XMLWriter(sw, format); 
        try { 
                xw.write(document); 
                xw.flush(); 
                xw.close(); 
        } catch (IOException e) { 
                log.error("格式化XML文档发生异常，请检查！" , e); 
        } 
        return sw.toString(); 
}

	
	/**
	 * 生成经营许可的xml
	 * @param map
	 * @param element
	 */
	private void creatJyxkXml(Map map,Element element){
		element.addElement("sqdw").addText((String)map.get("sqdw")==null?"":(String)map.get("sqdw"));
		element.addElement("address").addText((String)map.get("address")==null?"":(String)map.get("address"));
		element.addElement("post").addText((String)map.get("post")==null?"":(String)map.get("post"));
		element.addElement("clsj").addText((String)map.get("clsj")==null?"":(String)map.get("clsj"));
		element.addElement("gszcdjh").addText((String)map.get("gszcdjh")==null?"":(String)map.get("gszcdjh"));
		element.addElement("ywlxr").addText((String)map.get("ywlxr")==null?"":(String)map.get("ywlxr"));
		element.addElement("ywlxrtel").addText((String)map.get("ywlxrtel")==null?"":(String)map.get("ywlxrtel"));
		element.addElement("email").addText((String)map.get("email")==null?"":(String)map.get("email"));
		element.addElement("qywz").addText((String)map.get("qywz")==null?"":(String)map.get("qywz"));
		element.addElement("fddbr").addText((String)map.get("fddbr")==null?"":(String)map.get("fddbr"));
		element.addElement("fddbrZw").addText((String)map.get("fddbr_zw")==null?"":(String)map.get("fddbr_zw"));
		element.addElement("fddbrZc").addText((String)map.get("fddbr_zc")==null?"":(String)map.get("fddbr_zc"));
		element.addElement("zyfzr").addText((String)map.get("zyfzr")==null?"":(String)map.get("zyfzr"));
		element.addElement("zyfzrZw").addText((String)map.get("zyfzr_zw")==null?"":(String)map.get("zyfzr_zw"));
		element.addElement("zyfzrZc").addText((String)map.get("zyfzr_zc")==null?"":(String)map.get("zyfzr_zc"));
		element.addElement("aqfzr").addText((String)map.get("aqfzr")==null?"":(String)map.get("aqfzr"));
		element.addElement("aqfzrZw").addText((String)map.get("aqfzr_zw")==null?"":(String)map.get("aqfzr_zw"));
		element.addElement("aqfzrZc").addText((String)map.get("aqfzr_zc")==null?"":(String)map.get("aqfzr_zc"));
		element.addElement("cz").addText((String)map.get("cz")==null?"":(String)map.get("cz"));
		
		element.addElement("jsfzr").addText((String)map.get("jsfzr")==null?"":(String)map.get("jsfzr"));
		element.addElement("jsfzrZw").addText((String)map.get("jsfzr_zw")==null?"":(String)map.get("jsfzr_zw"));
		element.addElement("jsfzrZc").addText((String)map.get("jsfzr_zc")==null?"":(String)map.get("jsfzr_zc"));
		element.addElement("zczj").addText((String)map.get("zczj")==null?"":(String)map.get("zczj")+"万");
		element.addElement("qylx").addText((String)map.get("qylx")==null?"":(String)map.get("qylx"));
		element.addElement("jyfw").addText((String)map.get("jyfw")==null?"":(String)map.get("jyfw"));
		element.addElement("gqqy").addText((String)map.get("gqqy")==null?"":(String)map.get("gqqy"));
		element.addElement("fwdh").addText((String)map.get("fwdh")==null?"":(String)map.get("fwdh"));
		element.addElement("qxdh").addText((String)map.get("qxdh")==null?"":(String)map.get("qxdh"));
		element.addElement("qyjj").addText((String)map.get("qyjj")==null?"":(String)map.get("qyjj"));
		
	}
	
	private void creatYnYsqyXml(Map map,Element element){
		element.addElement("id").addText((String)map.get("id")==null?"":(String)map.get("id"));		
		element.addElement("sqdw").addText((String)map.get("sqdw")==null?"":(String)map.get("sqdw"));		
		element.addElement("qyjc").addText((String)map.get("qyjc")==null?"":(String)map.get("qyjc"));//企业简称		
		element.addElement("qyphone").addText((String)map.get("qyphone")==null?"":(String)map.get("qyphone"));		
		element.addElement("address").addText((String)map.get("address")==null?"":(String)map.get("address"));		
		element.addElement("post").addText((String)map.get("post")==null?"":(String)map.get("post"));		
		element.addElement("cz").addText((String)map.get("cz")==null?"":(String)map.get("cz"));//传真		
		element.addElement("frxm").addText((String)map.get("frxm")==null?"":(String)map.get("frxm"));		
		element.addElement("frphone").addText((String)map.get("frphone")==null?"":(String)map.get("frphone"));		
		element.addElement("ywlxr").addText((String)map.get("ywlxr")==null?"":(String)map.get("ywlxr"));		
		element.addElement("ywlxrtel").addText((String)map.get("ywlxrtel")==null?"":(String)map.get("ywlxrtel"));		
		element.addElement("zydm").addText((String)map.get("zydm")==null?"":(String)map.get("zydm"));		
		element.addElement("gszcdjh").addText((String)map.get("gszcdjh")==null?"":(String)map.get("gszcdjh"));		
		element.addElement("gszcdjyxq").addText((String)map.get("gszcdjyxq")==null?"":(String)map.get("gszcdjyxq"));		
		element.addElement("qydlyszh").addText((String)map.get("qydlyszh")==null?"":(String)map.get("qydlyszh"));		
		element.addElement("qydlyszyxq").addText((String)map.get("qydlyszyxq")==null?"":(String)map.get("qydlyszyxq"));		
		element.addElement("zzdj").addText((String)map.get("zzdj")==null?"":(String)map.get("zzdj"));//企业等级		
		element.addElement("cls").addText((String)map.get("cls")==null?"":(String)map.get("cls"));
	}
	
	private void creatYnYsclXml(Map map,Element element){
		element.addElement("id").addText((String)map.get("id")==null?"":(String)map.get("id"));		
		element.addElement("gsmc").addText((String)map.get("gsmc")==null?"":(String)map.get("gsmc"));		
		element.addElement("cjh").addText((String)map.get("cjh")==null?"":(String)map.get("cjh"));		
		element.addElement("cphm").addText((String)map.get("cphm")==null?"":(String)map.get("cphm"));		
		element.addElement("zyzh").addText((String)map.get("zyzh")==null?"":(String)map.get("zyzh"));		
		element.addElement("zyzyxq").addText((String)map.get("zyzyxq")==null?"":(String)map.get("zyzyxq"));		
		element.addElement("cx").addText((String)map.get("cx")==null?"":(String)map.get("cx"));			
		element.addElement("ccrq").addText((String)map.get("ccrq")==null?"":(String)map.get("ccrq"));		
		element.addElement("djrq").addText((String)map.get("djrq")==null?"":(String)map.get("djrq"));		
		element.addElement("fdjh").addText((String)map.get("fdjh")==null?"":(String)map.get("fdjh"));		
		element.addElement("zzl").addText((String)map.get("zzl")==null?"":(String)map.get("zzl"));		
		element.addElement("xszyxrq").addText((String)map.get("xszyxrq")==null?"":(String)map.get("xszyxrq"));		
		element.addElement("gpsno").addText((String)map.get("gpsno")==null?"":(String)map.get("gpsno"));		
		element.addElement("shr").addText((String)map.get("shr")==null?"":(String)map.get("shr"));		
		element.addElement("shrqStr").addText((String)map.get("shrqStr")==null?"":(String)map.get("shrqStr"));		
		element.addElement("remark").addText((String)map.get("remark")==null?"":(String)map.get("remark"));
	}
	
	/**
	 * 生成户外广告的xml
	 * @param map
	 * @param element
	 */
	private void creatHwggXml(Map map,Element element){
		String file_code=(String) map.get("file_code");
		String state=(String) map.get("state_");
		String result="";
		if("ended".equals(state)){
		   if("byxkjds".equals(file_code)){
			    	result="不予许可";
			    }else {
			    	result= "准予许可";
			}
		}else{
			result= "审批中";
		}
	
	    List list=this.getApplyContent((String)map.get("procinst_"));
		String content="";
		if(list.size()>0){
			for(int i=0;i<list.size();i++){
				Map data=(Map) list.get(i);
				if(StringUtils.isNotBlank((String)data.get("handle_comments"))){
				content+=(String)data.get("handle_comments")+"\n\r";
				content+=(String)data.get("assignee_name")+"\n\r";
				String time=(String)data.get("handle_comments_time");
				if(StringUtils.isNotBlank(time)){
					if(time.length()>=19){
						time=time.substring(0,19);
					}
				}
				content+=time;
				
				}
			}
		}
		element.addElement("code").addText((String)map.get("accept_no")==null?"":(String)map.get("accept_no"));
		element.addElement("name").addText((String)map.get("gd_name")==null?"":(String)map.get("gd_name"));
		element.addElement("cotent").addText("");
		element.addElement("location").addText((String)map.get("address")==null?"":(String)map.get("address"));
		element.addElement("request_name").addText((String)map.get("sqdw"));
		element.addElement("phone").addText((String)map.get("ywlxrtel"));
		element.addElement("address").addText((String)map.get("ggsdz")==null?"":(String)map.get("ggsdz"));
		element.addElement("legal_man").addText((String)map.get("frxm")==null?"":(String)map.get("frxm"));
		element.addElement("handling_man").addText((String)map.get("bus_memo6")==null?"":(String)map.get("bus_memo6"));
		element.addElement("advType").addText((String)map.get("ggxs")==null?"":(String)map.get("ggxs"));
		element.addElement("advUse").addText((String)map.get("ggxz")==null?"":(String)map.get("ggxz"));
		element.addElement("sitetype").addText((String)map.get("cdxz")==null?"":(String)map.get("cdxz"));
		element.addElement("length").addText((String)map.get("ggggoCd")==null?"":(String)map.get("ggggoCd"));
		element.addElement("width").addText((String)map.get("ggggoKd")==null?"":(String)map.get("ggggoKd"));
		element.addElement("thick").addText((String)map.get("ggggoHd")==null?"":(String)map.get("ggggoHd"));
//		element.addElement("district").addText((String)map.get(""));
//		element.addElement("street").addText((String)map.get(""));
//		element.addElement("community").addText((String)map.get(""));
//		element.addElement("workGrid").addText((String)map.get(""));
//		
//		element.addElement("cellGrid").addText((String)map.get(""));
//		element.addElement("geo_x").addText((String)map.get(""));
//		element.addElement("geo_y").addText((String)map.get(""));
//		element.addElement("abs_x").addText((String)map.get(""));
//		element.addElement("abs_y").addText((String)map.get(""));
		element.addElement("license_time_begin").addText((String)map.get("nszqsTime")==null?"":(String)map.get("nszqsTime"));
		element.addElement("license_time_end").addText((String)map.get("nszzzTime")==null?"":(String)map.get("nszzzTime"));
		element.addElement("spCode").addText((String)map.get("accept_no")==null?"":(String)map.get("accept_no"));
		element.addElement("spConent").addText(content);
		
		element.addElement("spStatu").addText(result);
		//element.addElement("spUser").addText((String)map.get(""));
		element.addElement("spTime").addText((String)map.get("sarq"));		
		
	}
	
	
	private List getApplyContent(String procinstId) {
		String sql="  select * from jbpm4_hist_task t where t.procinst_='"+procinstId+"' order by t.dbid_ asc";
		List list=this.ExChangeDataDao.queryForList(sql);
		return list;
	}

	/**
	 * 户外广告的数据
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	private List getHwgg(String pageNo, String pageSize){
		Page page=new Page();
		page.setPageNo(Integer.valueOf(pageNo));
		page.setPageSize(Integer.valueOf(pageSize));
		String sql=this.getHwggSql();
		return this.ExChangeDataDao.searchByPage(sql, page, null);
		
	}
	
	/**
	 * 经营许可的数据
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	private List getJyxk(String pageNo, String pageSize){
		Page page=new Page();
		page.setPageNo(Integer.valueOf(pageNo));
		page.setPageSize(Integer.valueOf(pageSize));
		String sql=this.getJyxkSql();
		return this.ExChangeDataDao.searchByPage(sql, page, null);
		
	}
	
	private List getYnYsqy(String pageNo, String pageSize){
		Page page=new Page();
		page.setPageNo(Integer.valueOf(pageNo));
		page.setPageSize(Integer.valueOf(pageSize));
		String sql=this.getYnYsqySql();
		return this.ExChangeDataDao.searchByPage(sql, page, null);		
	}
	
	private List getYnYscl(String pageNo, String pageSize){
		Page page=new Page();
		page.setPageNo(Integer.valueOf(pageNo));
		page.setPageSize(Integer.valueOf(pageSize));
		String sql=this.getYnYsclSql();
		return this.ExChangeDataDao.searchByPage(sql, page, null);		
	}
	
	
	/**
	 * 户外广告的sql
	 * @return
	 */
	private String getHwggSql(){
		StringBuffer sb=new StringBuffer();
		sb.append(" select t.*,doc.file_code from(select distinct \r");
		sb.append("  hwggsqb.*,qyxx.address,qyxx.ywlxrtel,\r");
		sb.append("temp.template_code, ins.bus_date2, ins.bus_date1,ins.bus_memo1,ins.bus_memo3, \r");
		sb.append("  temp.template_name,ins.master_entity_key,proc.start_ ,ins.wf_bus_instance_id,proc.state_,task.procinst_ from \r");
		sb.append(" JBPM4_HIST_TASK task,JBPM4_HIST_PROCINST proc,WF_BUS_INSTANCE ins,WF_BUS_TEMPLATE temp,YW_JG_HWGG_SQB hwggsqb \r");
		sb.append("  ,yw_com_qyxx qyxx where task.procinst_=proc.id_ and proc.id_=ins.proc_inst_id \r");
		sb.append("  and ins.template_id=temp.id   and hwggsqb.id=ins.master_entity_key and hwggsqb.accept_no=ins.bus_memo3 \r");
		sb.append("  and ( proc.state_='ended' or  proc.state_='active') and qyxx.id=hwggsqb.qyid \r");
		sb.append("  and qyxx.template_code=temp.template_code and temp.template_code in ('hwggshsqb')   order by proc.start_ desc \r");
		sb.append("  ) t left join YW_PRINT_DOC doc on doc.yw_id=t.master_entity_key and doc.template_code=t.template_code  \r");
		sb.append("  and doc.yw_id=t.id  and doc.file_code in ('byxkjds','bzcltzs','zyxzxkjds') \r");
		return sb.toString();
	}
	
	
	
	private String getJyxkSql(){
		StringBuffer sb=new StringBuffer();
		sb.append(" select t.*,doc.file_code from(select distinct \r");
		sb.append("  hwggsqb.*,qyxx.post,qyxx.gszcdjh,qyxx.ywlxr,qyxx.ywlxrtel,qyxx.email,qyxx.zczj, \r");
		sb.append("  qyxx.address,");
		sb.append("temp.template_code, ins.bus_date2, ins.bus_date1,ins.bus_memo1,ins.bus_memo3, \r");
		sb.append("  temp.template_name,ins.master_entity_key,proc.start_ ,ins.wf_bus_instance_id,proc.state_,task.procinst_ from \r");
		sb.append(" JBPM4_HIST_TASK task,JBPM4_HIST_PROCINST proc,WF_BUS_INSTANCE ins,WF_BUS_TEMPLATE temp,yw_rq_qyjyxk hwggsqb \r");
		sb.append("  ,yw_com_qyxx qyxx where task.procinst_=proc.id_ and proc.id_=ins.proc_inst_id \r");
		sb.append("  and ins.template_id=temp.id   and hwggsqb.id=ins.master_entity_key and hwggsqb.accept_no=ins.bus_memo3 \r");
		sb.append("  and  proc.state_='ended' and qyxx.id=hwggsqb.qyid and qyxx.template_code=temp.template_code \r");
		sb.append("  and temp.template_code in ('rqjyxkz_pzqy','rqjyxkz_gdqy')  order by proc.start_ desc \r");
		sb.append("  ) t inner join YW_PRINT_DOC doc on doc.yw_id=t.master_entity_key and doc.template_code=t.template_code  \r");
		sb.append("  and doc.yw_id=t.id  and doc.file_code='zyxzxkjds' \r");
		return sb.toString();
	}
	
	public String getYnYsqySql(){
		StringBuffer sb=new StringBuffer();
		sb.append("select t.*,qyxx.qyjc,qyxx.cz,qyxx.zzdj from YW_YN_JZFQWCZC_JBXX t,YW_COM_QYXX qyxx where t.sqsx='新增' and t.qyid=qyxx.id");
		return sb.toString();
	}
	
	public String getYnYsclSql(){
		StringBuffer sb=new StringBuffer();
		sb.append("select t.* from YW_YN_BUSINFO t");
		System.out.println("sb = "+sb.toString());
		return sb.toString();
	}

	@Override
	public String getRqssgd(String pageNo, String pageSize) {
		// TODO Auto-generated method stub
		return null;
	}


}
